Toggle spellcheck with function keys
If you have gvim toolbars disabled, here's an easier and quicker way to toggle the spellcheck functionality: :map :setlocal spell! spelllang=en_us Remap to whichever Fx key you require, or switch the dictionary to use. Comments Also for when you're in insert mode: ":imap :setlocal spell! spelllang=en_usi inoremap :setlocal spelllang=en_us spell! spell? ---- This works better. The previous tip's :imap will change the position of the cursor if the cursor was at the very end of the line. :imap :setlocal spell! spelllang=en_us I just used the following: nn :setlocal spell! spell? is the same as most Windows applications. spelllang=en_us is by default? ---- " For those with sadly no function keys available " toggle spelling use \s imap s :setlocal spell! spelllang=en_gb nmap s :setlocal spell! spelllang=en_gb let g:myLang = 0 let g:myLangList = 'de_de', 'en_gb' function! MySpellLang() "loop through languages if g:myLang 0 | setlocal nospell | endif if g:myLang 1 | let &l:spelllang = g:myLangListg:myLang | setlocal spell | endif if g:myLang 2 | let &l:spelllang = g:myLangListg:myLang | setlocal spell | endif echomsg 'language:' g:myLangListg:myLang let g:myLang = g:myLang + 1 if g:myLang >= len(g:myLangList) | let g:myLang = 0 | endif endfunction map :call MySpellLang() Someone can maybe implement it a better way, but so I can toggle through. ---- Fiddled around a little, now it does not move the cursor anymore, and I don't have the slightest idea why. Here is that part from my .vimrc: "switch spellcheck languages let g:myLang = 0 let g:myLangList = [ "nospell", "de_de", "en_us" ] function! MySpellLang() "loop through languages let g:myLang = g:myLang + 1 if g:myLang >= len(g:myLangList) | let g:myLang = 0 | endif if g:myLang 0 | set nospell | endif if g:myLang 1 | setlocal spell spelllang=de_de | endif if g:myLang 2 | setlocal spell spelllang=en_us | endif echo "language:" g:myLangListg:myLang endf map :call MySpellLang() imap :call MySpellLang() Don't know what I changed, but the most important thing is that it works now. I also changed the order of the commands, because before the first hit of F7 would only display the current state and did not change anything, now it directly switches to the next language. ---- It would be better to set the "myLang" variable local for each buffer, because the spell option is also local. In case you use two or more buffers and activate "en_gb" for one, "g:myLang" contains 2 now if you want to activate it for another one, you have to press three times. So change each occurrence of g:myLang to b:myLang and define it for each buffer when the function is called for the first time. For this: remove the "let g:myLang = 0" line and add the following statement to the function directly after the function declaration: if !exists( "b:myLang" ) let b:myLang=0 endif The spell option can be directly set to get(g:myLangList, b:myLang) (except for the case b:myLang 0, of course). This way, the list can be managed and accessed more easily: if b:myLang 0 setlocal nospell else execute "setlocal spell spelllang=" . get(g:myLangList, b:myLang) endif Putting it all together: " Spell Check let b:myLang=0 let g:myLangList="nospell","de_de","en_gb" function! ToggleSpell() let b:myLang=b:myLang+1 if b:myLang>=len(g:myLangList) | let b:myLang=0 | endif if b:myLang 0 setlocal nospell else execute "setlocal spell spelllang=".get(g:myLangList, b:myLang) endif echo "spell checking language:" g:myLangListb:myLang endfunction nmap :call ToggleSpell() ---- In case the spelling language was set by other means than ToggleSpell() (a filetype autocommand say), it is safer to replace if !exists( "b:myLang" ) let b:myLang=0 endif by this additional check if !exists( "b:myLang" ) if &spell let b:myLang=index(g:myLangList, &spelllang) else let b:myLang=0 endif endif Note that index returns -1 if the set language is not in g:myLangList, implying the spelling to be toggled off at this occasion.